package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    //发送手机验证码
    @PostMapping("/user/login")
    public void sendSmsCode(@RequestBody Map<String, String> map) {
        //1. 接收参数
        String phone = map.get("phone");

        //2. 调用manager
        userManager.sendSmsCode(phone);
    }

    //登录注册
    @PostMapping("/user/loginVerification")
    public Map<String, Object> loginOrReg(@RequestBody Map<String, String> map) {
        //1. 接收参数
        String phone = map.get("phone");
        String verificationCode = map.get("verificationCode");

        //2. 调用manager
        Map<String, Object> resultMap = userManager.loginOrReg(phone, verificationCode);

        //3. 返回结果
        return resultMap;
    }


    //完善个人基本信息
    @PostMapping("/user/loginReginfo")
    public void saveUserBaseInfo(@RequestBody UserInfo userInfo) {
        //1. 解析token
        User user = UserHolder.get();

        //2. 向userInfo设置userId
        userInfo.setId(user.getId());

        //3. 调用manager
        userManager.saveUserBaseInfo(userInfo);
    }

    //完善个人头像信息
    //注意文件类型必须是MultipartFile,而且不能使用@RequestBody修饰
    @PostMapping({"/user/loginReginfo/head", "/users/header"})
    public void saveUserHeaderInfo(MultipartFile headPhoto) throws IOException {
        //1. 获取token中的userId
        Long userId = UserHolder.get().getId();

        //2. 调用manager
        userManager.saveUserHeaderInfo(userId, headPhoto);
    }

    //查询个人信息
    @GetMapping("/users")
    public UserInfoVo findUserInfo(
            @RequestParam(value = "userID", required = false) Long userId,
            @RequestParam(value = "huanxinID", required = false) Long huanxinId
    ) {
        if (userId != null) {
            return userManager.findByUserId(userId);
        } else if (huanxinId != null) {
            return userManager.findByUserId(huanxinId);
        } else {
            Long uid = UserHolder.get().getId();
            return userManager.findByUserId(uid);
        }
    }


    //更新个人基本信息
    @PutMapping("/users")
    public void updateUserBaseInfo(@RequestBody UserInfo userInfo) {
        userManager.updateUserBaseInfo(userInfo);
    }

    //修改手机号 发送验证码
    @PostMapping("/users/phone/sendVerificationCode")
    public void sendSmsCodes() {
        //获取当前手机号
        String phone = UserHolder.get().getPhone();

        userManager.sendSmsCode(phone);
    }

    //校验验证码
    @PostMapping("/users/phone/checkVerificationCode")
    public Map<String,Boolean> checkVerificationCode(@RequestBody Map<String, String> map) {
        //获取输入的验证码
        String verificationCode = map.get("verificationCode");
        //获取当前用户的手机号
        String phone = UserHolder.get().getPhone();

        Map<String, Boolean> code = userManager.checkVerificationCode(verificationCode, phone);

        return code;
    }

    //保存
    @PostMapping("/users/phone")
    public void updatePhone(@RequestBody Map<String,String> map){
        //将修改后的手机号进行保存到数据库中
        String phone=map.get("phone");
        userManager.updatePhone(phone);
    }

}
